/*
 * 
 */
package com.flying.dcss.dcauthorization.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.common.service.impl.MailService;
import com.flying.dcss.common.service.impl.SmsService;
import com.flying.dcss.dcauthorization.bo.impl.DcssApplicationOrder;
import com.flying.dcss.dcauthorization.service.impl.AuthorizeService;
import com.flying.dcss.dcmanager.dao.impl.DcssDCInfoDAO;
import com.flying.dcss.dcmanager.service.impl.DCInfoService;
import com.flying.dcss.userpermissions.service.impl.UserInfoService;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

// TODO: Auto-generated Javadoc
/**
 * The Class CreateFormAction.
 */
public class CreateFormAction extends ActionSupport {

	/** The authorize service. */
	private AuthorizeService authorizeService;

	/** The user service. */
	private UserService userService;

	/** The user info service. */
	private UserInfoService userInfoService;

	/** The mail service. */
	private MailService mailService;

	private SmsService smsService;

	public void setSmsService(SmsService smsService) {
		this.smsService = smsService;
	}

	private DCInfoService dcInfoService;

	public void setdcInfoService(DCInfoService dcInfoService) {
		this.dcInfoService = dcInfoService;
	}

	/**
	 * Sets the mail service.
	 * 
	 * @param mailService
	 *            the new mail service
	 */
	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	/**
	 * Sets the authorize service.
	 * 
	 * @param authorizeService
	 *            the new authorize service
	 */
	public void setAuthorizeService(AuthorizeService authorizeService) {
		this.authorizeService = authorizeService;
	}

	/**
	 * Sets the user service.
	 * 
	 * @param userService
	 *            the new user service
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * Sets the user info service.
	 * 
	 * @param userInfoService
	 *            the new user info service
	 */
	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub

		// Get data.
		String currentUserId = ServletActionContext.getRequest().getParameter(
				"currentUserId");

		if (currentUserId == null) {
			return null;
		}
		String dcId = ServletActionContext.getRequest().getParameter("dcId");
		String reason = ServletActionContext.getRequest()
				.getParameter("reason");

		// Convert String to Date.
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date deadline = format.parse(ServletActionContext.getRequest()
				.getParameter("deadline"));

		String approverEmail = ServletActionContext.getRequest().getParameter(
				"approverEmail");

		// Save.
		DcssApplicationOrder model = new DcssApplicationOrder(UUID.randomUUID()
				.toString(), currentUserId, dcId, reason, userService
				.getUserByEmail(approverEmail).getId(), deadline, 0);

		authorizeService.addApplicationOrder(model);

		// Send notice email.
		// mailService.sendOrderNoticeEmail(approverEmail,
		// userInfoService.getUserInfoByUserId(currentUserId));

		// userService.getUserByEmail(approverEmail).getPhoneNum();
		// userService.getUserByEmail(approverEmail).getUsername();
		// userInfoService.getUserInfoByUserId(currentUserId).getRealName();
		// dcInfoService.getById(dcId).getDescription();

		// Send notice sms.
		smsService.sendCreateOrderSMS(userService.getUserByEmail(approverEmail)
				.getPhoneNum(), userService.getUserByEmail(approverEmail)
				.getUsername(), userInfoService.getUserInfoByUserId(
				currentUserId).getRealName(), " ");

		return this.SUCCESS;
	}
}
